package com.kaiwukj.android.ufamily.mvp.ui.page.mine.face;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.zghl.mclient.client.ZghlMClient;
import com.zghl.mclient.client.ZghlStateListener;
import com.zghl.mclient.client.beans.HttpDataType;
import com.zghl.mclient.client.utils.NetDataFormat;

import java.io.File;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.FacePrewRouterUrl)
public class FacePrewActivity extends BaseActivity {

    private static final int FACE_GET = 106;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_face)
    ImageView ivFace;
    @BindView(R.id.btn_submit)
    QMUIRoundButton btnSubmit;

    private boolean changed = false;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_face_prew;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("上传人脸");
        if (!ZghlMClient.getInstance().hasFace()) {
            btnSubmit.setText("去上传");
        } else {
            btnSubmit.setText("重新拍摄");
            refreshFace();
        }
        btnSubmit.setOnClickListener(v -> {
            ARouter.getInstance().build(ARouterUrlKt.ViseFaceActivityUrl)
                    .navigation(this, FACE_GET);
        });

    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    private void refreshFace() {
        ZghlMClient.getInstance().getFaceMsg(new ZghlStateListener() {
            @Override
            public void onSuccess(int i, String s) {
                Map<String, Object> rltMap = NetDataFormat.getDataByT(new HttpDataType<Map<String, Object>>() {
                }, s);
                String uid = (String) rltMap.get("uid");
                String updateTime = (String) rltMap.get("update_time");
                String faceImageUrl = (String) rltMap.get("face_image");
                String source = (String) rltMap.get("source");
                showFace(faceImageUrl);
                //  LogUtils.d("rltMap----------------->" + GsonUtils.toJson(rltMap));
                //  LogUtils.d("onSuccess---------------->" + i + "---" + s);
            }

            @Override
            public void onError(int i, String s) {
                // LogUtils.d("onError---------------->" + i + "---" + s);
            }
        });
    }

    private void showFace(String imageUrl) {
        if (isFinishing()) return;
        if (!StringUtils.isEmpty(imageUrl)) {
            Glide.with(this)
                    .load(imageUrl)
                    .placeholder(R.drawable.picture_image_placeholder)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(ivFace);
        }
    }

    private void showFaceByLocal(String imagePath) {
        if (isFinishing()) return;
        //LogUtils.d("imagePath---------------->" + imagePath);
        File faceFile = new File(imagePath);
        if (faceFile.exists() && faceFile.isFile()) {
            Glide.with(this)
                    .load(faceFile)
                    .into(ivFace);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == FACE_GET) {
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    String facePath = data.getStringExtra("face");
                    faceRegister(facePath);
                }
            }
        }
    }

    @Override
    public void onBackPressedSupport() {
        if (changed) {
            setResult(RESULT_OK);
            finish();
        } else {
            super.onBackPressedSupport();
        }
    }

    private void faceRegister(String imgPath) {
        showLoading("正在处理...");
        LogUtils.d("imagePath------------------>" + imgPath);
        ZghlMClient.getInstance().faceRegister(imgPath, new ZghlStateListener() {
            @Override
            public void onSuccess(int i, String s) {
                //  ZghlMClient.getInstance().setHasFace(true);
                runOnUiThread(() -> {
                    hideLoading();
                    showMessage("采集成功");
                    changed = true;
                    //showFaceByLocal(imgPath);
                    refreshFace();
                });
            }

            @Override
            public void onError(int i, String s) {
                LogUtils.e("onError:" + i + ":" + s);
                runOnUiThread(() -> {
                    hideLoading();
                    showMessage("采集失败");
                });
            }
        });
    }
}
